package com.xiaoxin.experience.service.tree.model;

import com.xiaoxin.experience.service.tree.common.AnyTree;
import com.xiaoxin.experience.service.tree.common.BranchProxy;
import com.xiaoxin.experience.service.tree.common.LeafProxy;
import com.xiaoxin.experience.util.StringUtil;

import java.util.List;

/**
 * @author xiaoxin
 */
public class ApiTree extends AnyTree<ApiDirectory,Api,String> {

    public ApiTree(List<ApiDirectory> list)
    {
        super(new ApiDirectoryProxy(), new ApiProxy());
        createTree(list);
    }

    public ApiTree(List<ApiDirectory> list, ApiDirectory backRoot, boolean forceRoot)
    {
        super(new ApiDirectoryProxy(), new ApiProxy());
        createTree(list, backRoot, forceRoot);
    }


    private static class ApiDirectoryProxy implements BranchProxy<ApiDirectory,String>
    {

        @Override
        public String branchMark(ApiDirectory object)
        {
            return object.getId();
        }

        @Override
        public String branchFrom(ApiDirectory object)
        {
            return object.getParentId();
        }

        @Override
        public void branchReset(ApiDirectory object, String parent)
        {
            object.setParentId(StringUtil.isBlank(parent) ? "-1" : parent);
        }
    }

    private static class ApiProxy implements LeafProxy<Api,String>
    {
        @Override
        public String branchMark(Api object)
        {
            return object.getDirectoryId();
        }
    }
}
